import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/constant/point_constant.dart';
import 'package:distribution_mall_flutter/entity/order/order.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/provider/order/common.dart';
import 'package:distribution_mall_flutter/utils/firebase/firebase_analytics_utils.dart';
import 'package:distribution_mall_flutter/widgets/modal_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:getwidget/components/radio_list_tile/gf_radio_list_tile.dart';

class OrderCancel extends ConsumerWidget {
  final String distributionOrderId;
  final List<SaleReasonEntity> list;
  final Function() onFooterPressed;
  const OrderCancel(
      {super.key,
      required this.list,
      required this.onFooterPressed,
      required this.distributionOrderId});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final commonModal = ref.watch(orderCommonProvider);
    String value =
        ref.watch(orderCommonProvider.select((value) => value.value));
    bool loading =
        ref.watch(orderCommonProvider.select((value) => value.loading));
    return EPModalButton(
        title: S.current.order_reason_cancel,
        isLoading: loading,
        maxHeightScale: 0.8,
        buttonText: S.current.confirm,
        headerCrossAxis: CrossAxisAlignment.start,
        footerMargin:
            const EdgeInsets.symmetric(horizontal: EdgeConstant.edge218),
        onFooterPressed: value.isNotEmpty
            ? () {
                ref.read(orderCommonProvider.notifier).orderCancel({
                  'distributionOrderId': distributionOrderId,
                  'cancelReason': value,
                }).then((bol) {
                  FirebaseAnalyticsUtils.logEvent(
                      name: PointConstant.orderCancel,
                      parameters: {
                        'orderNo': distributionOrderId,
                        'cancelReason': value,
                        'success': bol ? 1 : 0
                      });
                  if (bol) {
                    onFooterPressed();
                    Navigator.pop(context);
                  }
                });
              }
            : null,
        hasDefaultFooter: true,
        padding: const EdgeInsets.fromLTRB(0, 23.7, 0, EdgeConstant.edge10),
        children: list
            .map((item) => GFRadioListTile(
                  onChanged: (value) {
                    ref.read(orderCommonProvider.notifier).setValue(value);
                  },
                  value: item.reasonEn,
                  groupValue: value,
                  size: 19,
                  margin: EdgeInsets.zero,
                  padding: const EdgeInsets.symmetric(
                      horizontal: EdgeConstant.edge218,
                      vertical: EdgeConstant.edge20),
                  radioColor: Colors.white,
                  activeBgColor: ColorConstant.primaryColor,
                  title: Text(
                    item.reasonEn ?? '',
                    style: const TextStyle(
                        color: Colors.black87, fontSize: FontConstant.f146),
                  ),
                ))
            .toList());
  }
}
